package com.atguigu.gmall.cart.rpc;

import com.atguigu.gmall.cart.entity.CartInfo;
import com.atguigu.gmall.cart.service.CartService;
import com.atguigu.gmall.common.config.utils.UserAuthUtil;
import com.atguigu.gmall.common.constant.RedisConst;
import com.atguigu.gmall.common.execption.GmallException;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.product.entity.SkuInfo;
import org.apache.catalina.util.RequestUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Description
 * @Return
 * @Author https://weiranyi.github.io
 * @Date Created in 2022-12-16 12:16
 **/
@RequestMapping("/api/inner/rpc/cart")
@RestController
public class CartRpcController {
    @Autowired
    CartService cartService;


    @GetMapping("/add/{skuId}")
    public Result<SkuInfo> addToCart(@PathVariable("skuId") Long skuId,
                                     @RequestParam("skuNum") Integer num
    ) {
        //1、判断用哪个购物车【临时的 还是 用户的】
        String cartKey = cartService.determinCaryKey();
        //2、把指定商品添加到指定购物车
        SkuInfo skuInfo = cartService.addToCart(skuId, num, cartKey);
        return Result.ok(skuInfo);
    }

    @GetMapping("/del/checked")
    public Result delChecked() {
        String caryKey = cartService.determinCaryKey();
        cartService.delChecked(caryKey);
        return Result.ok();
    }

    // 订单-下单业务相关--------------------------

    /**
     * 获取购物车中选中得商品
     *
     * @return
     */
    @GetMapping("/checked/infos")
    public Result<List<CartInfo>> getCheckedCartInfo() {
        //1、判断用哪个购物车【临时的 还是 用户的】
        String cartKey = cartService.determinCaryKey();
        List<CartInfo> cartInfos = cartService.getChecked(cartKey);
        return Result.ok(cartInfos);
    }

    @PostMapping("/delete/skuIds")
    public Result deleteSkuIds(@RequestBody List<Long> skuIds) {
        //1、判断用哪个购物车【临时的 还是 用户的】
        String cartKey = cartService.determinCaryKey();
        cartService.deleteSkuIds(cartKey,skuIds);
        return Result.ok();
    }
}
